<?php 
namespace app\index\service;

class Club extends \app\index\service\Pub
{
    protected $appid = 'wx18a97b2d80c7e18f';
    protected $secret= 'e6679fc108f4915cf5f14c25214d5504';
    
    public function getAppid()
    {
        return $this->appid;
    }
    
    public function getSecret()
    {
        return $this->secret;
    }
    
    public function login($code)
    {
        $encrypt = new \app\index\service\Encrypt();
        //echo $encrypt->decrypt('X2bRtH5bwwTCVbpTO0GRYA==');exit;
        //echo $encrypt->encrypt('123');exit;
        $res = file_get_contents('https://api.weixin.qq.com/sns/jscode2session?appid='.$this->getAppid().'&secret='.$this->getSecret().'&js_code='.$code.'&grant_type=authorization_code');
        if($res){
            $res = json_decode($res, true);
            if(isset($res['openid']) && $res['openid']){
                $customer = $this->getCustomerByOpenId($res['openid']);
                $data = [];
                $data['token'] = $encrypt->encrypt(json_encode([
                    'openid'=>$res['openid'],
                    'uid'=>$customer->id
                ]));
                $data['name'] = $customer->name??'';
                $data['telephone'] = $customer->telephone??'';
                return $data;
            }
            if(isset($res['errmsg']) && $res['errmsg']){
                throw new \Exception($res['errmsg']);
            }
            throw new \Exception('jscode2session接口异常'.print_r($res, true));
        }else{
            throw new \Exception('jscode2session接口异常');
        }
    }
    
    public function getCustomerByOpenId($openid)
    {
        $student = new \app\admin\model\club\Student();
        $item = $student->getQuery(true)->where('openid', $openid)->find();
        if($item){
            return $item;
        }else{
            $time = date('Y-m-d H:i:s');
            $id = $student->getQuery(true)->insertGetId(['openid'=>$openid,'telephone'=>'','updated_at'=>$time,'created_at'=>$time]);
            return $student->getQuery(true)->where('id', $id)->find();
        }
    }
    
    public function getLoginUid()
    {
        $token = $_REQUEST['token']??'';
        if($token){
            $encrypt = new \app\index\service\Encrypt();
            $arr = $encrypt->decrypt($token);
            if($arr){
                $arr = json_decode($arr, true);
                if(isset($arr['uid']) && $arr['uid']){
                    return $arr['uid'];
                }
            }
            throw new \Exception('登录失败');
        }else{
            throw new \Exception('登录失败token不能为空');
        }
    }
    
    public function updateCustomer($id, $up)
    {
        if($up){
            $student = new \app\admin\model\club\Student();
            $student->getQuery(true)->where('id', $id)->update($up);
        }
    }
    
    public function getCustomerInfo($id)
    {
        $student = new \app\admin\model\club\Student();
        return $student->getQuery(true)->where('id', $id)->find();
    }
    
    public function getCourseList($type='')
    {
        $course = new \app\admin\model\club\Course();
        $query = $course->getQuery(true);
        if($type == '首页'){
            $query->where('show_in_front', '开启');
        }
        $items = $query->select();
        foreach ($items as $item){
            $item['banner'] = $this->addServerUrl($item['banner']);
        }
        return $items;
    }
    
    public function getCourseListHistory($category)
    {
        $course = new \app\admin\model\club\Course();
        $query = $course->getQuery(true);
        $query->where('status', '关闭');
        $items = $query->select();
        foreach ($items as $item){
            $item['banner'] = $this->addServerUrl($item['banner']);
            $item['photos']  = $this->getMultiImages($item['photos']);
            $item['teacher'] = $this->getCoachDetail($item['teacher_id']);
        }
        return $items;
    }
    
    public function getCourseDetail($course_id='')
    {
        $course = new \app\admin\model\club\Course();
        $query = $course->getQuery(true);
        $query->where('id', $course_id);
        $items = $query->find();
        $items['banner']  = $this->addServerUrl($items['banner']);
        $items['photos']  = $this->getMultiImages($items['photos']);
        $items['teacher'] = $this->getCoachDetail($items['teacher_id']);
        return $items;
    }
    
    public function getCoachDetail($id='')
    {
        $course = new \app\admin\model\club\Teacher();
        $query = $course->getQuery(true);
        $query->where('id', $id);
        $items = $query->find();
        $items['avatar'] = $this->addServerUrl($items['avatar']);
        $items['avatar_big'] = $this->addServerUrl($items['avatar_big']);
        return $items;
    }
    
    public function getCommentList($teacher_id, $type='教练')
    {
        $comment = new \app\admin\model\club\Comment();
        $query = $comment->getQuery(true);
        $query->where('type', $type);
        $query->where('parent_id', $teacher_id);
        return $query->select();
    }
    
    public function getCourseAddComment($uid, $course_id, $text)
    {
        $customer = $this->getCustomerInfo($uid);
        $course = $this->getCourseDetail($course_id);
        $comment = new \app\admin\model\club\Comment();
        $time    = date('Y-m-d');
        return $comment->getQuery(true)->insertGetId([
            'type'=>'课程',
            'parent_id'=>$course_id,
            'title'=>$course->name??"",
            'name'=>$customer->name??'',
            'content'=>$text,
            'status'=>'显示',
            'created_at'=>$time,
            'updated_at'=>$time
        ]);
    }
    
    public function getCoachAddComment($uid, $teacher_id, $text)
    {
        $customer = $this->getCustomerInfo($uid);
        $teacher = $this->getCoachDetail($teacher_id);
        $comment = new \app\admin\model\club\Comment();
        $time    = date('Y-m-d');
        return $comment->getQuery(true)->insertGetId([
            'type'=>'教练',
            'parent_id'=>$teacher_id,
            'title'=>$teacher->name??'',
            'name'=>$customer->name??'',
            'content'=>$text,
            'status'=>'显示',
            'created_at'=>$time,
            'updated_at'=>$time
        ]);
    }
    
    public function getCustomerOrderList($uid, $status)
    {
        $item = new \app\admin\model\club\Order();
        $query = $item->getQuery(true);
        $query->where('student_id', $uid);
        if($status){
            $query->where('status', $status);
        }
        return $query->select();
    }
    
    public function getOrderDetail($id)
    {
        $item = new \app\admin\model\club\Order();
        $query = $item->getQuery(true);
        $query->where('id', $id);
        return $query->find();
    }
    
    public function placeOrder($uid, $course_id)
    {
        $order = new \app\admin\model\club\Order();
        if($order->getQuery(true)->where('course_id', $course_id)->where('student_id', $uid)->where('status', '<>', '已取消')->count() > 0){
            throw new \Exception('您已报名当前课程，请勿重复报名');
        } 
        $customer = $this->getCustomerInfo($uid);
        $course   = $this->getCourseDetail($course_id);
        $teacher  = $this->getCoachDetail($course->teacher_id);
        $time    = date('Y-m-d');
        $oid = $order->getQuery(true)->insertGetId([
            'course_id'=>$course_id,
            'student_id'=>$uid,
            'teacher_name'=>$teacher->name??'',
            'teacher_telephone'=>$teacher->telephone??'',
            'student_name'=>$customer->name??"",
            'student_telephone'=>$customer->telephone??"",
            'course_name'=>$course->name??'',
            'course_price'=>$course->price,
            'course_num'=>$course->course_num,
            'course_discount'=>0,
            'pay_amount'=>$course->price,
            'status'=>'未付款',
            'created_at'=>$time,
            'updated_at'=>$time
        ]);
        return $this->getOrderDetail($oid);
    }
    
    
}